package com.hiyee.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.hiyee.exception.GraceException;
import com.hiyee.grace.result.ResponseStatusEnum;
import com.hiyee.pojo.AppUser;
import com.hiyee.service.BaseService;
import com.hiyee.user.mapper.AppUserMapper;
import com.hiyee.user.service.AppUserService;
import com.hiyee.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * 客户业务
 *
 */
@Service
public class AppUserServiceImpl extends BaseService implements AppUserService {

    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 根据手机号查询客户
     *
     * @param
     * @return
     */
    public PagedGridResult getList(String nickname, Integer status, Date start_date, Date end_date, Integer page, Integer page_size) {
        Example example = new Example(AppUser.class);
        example.orderBy("createdTime").desc();
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(nickname)) {
            criteria.andLike("nickname", "%" + nickname + "%");
        }
        if(status != null) {
            criteria.andEqualTo("activeStatus", status);
        }
        if(start_date != null) {
            criteria.andGreaterThanOrEqualTo("createdTime", start_date);
        }
        if(end_date != null) {
            criteria.andLessThanOrEqualTo("createdTime", end_date);
        }
        PageHelper.startPage(page, page_size);
        return this.getPageResult(appUserMapper.selectByExample(example), page, page_size);
    }

    @Override
    public AppUser getById(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public Integer freeze(String userId) {
        AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
        if(appUser == null) {
            GraceException.display(ResponseStatusEnum.USER_NOT_FOUND);
        }
        if(appUser.getActiveStatus() == 2) {
            appUser.setActiveStatus(1);
        } else if (appUser.getActiveStatus() == 1) {
            appUser.setActiveStatus(2);
        }
        return appUserMapper.updateByPrimaryKeySelective(appUser);
    }
}
